package com.xh1024.demo.ddns.ddnsservice.config;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsRequest;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainRecordsResponse;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordRequest;
import com.aliyuncs.alidns.model.v20150109.UpdateDomainRecordResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.xh1024.demo.ddns.ddnsservice.utils.HttpUtil;
import com.xh1024.demo.ddns.ddnsservice.utils.LogUtil;

import java.util.ResourceBundle;

/**
 * @description:
 * @author: wang zhen
 * @create: 2024-03-15 14:21
 */
public class AliyunDDNSTask {

    private static ResourceBundle bundle = ResourceBundle.getBundle("application");

    public static final String IP_QUERY_DOMAIN = bundle.getString("ip-query-domain");

    //#API调用的密钥对
    public static final String ACCESS_KEY = bundle.getString("aliyun.accessKey");
    public static final String ACCESS_KEY_Secret = bundle.getString("aliyun.accessKeySecret");

    //要做修改的域名,例如abc.com
    public static final String DOMAIN = bundle.getString("aliyun.domain");

    //主机记录名，例如www.baidu.com的主机记录名是www，home.abc.com的主机记录名是home，只想修改主域名可填写@
    public static final String HOME = bundle.getString("aliyun.domain-home");


    public static void DDNSTask() {

        try {

            // 查询指定二级域名的最新解析记录
            DefaultProfile profile = DefaultProfile.getProfile(
                    "cn-hangzhou",// 地域ID
                    ACCESS_KEY,// 您的AccessKey ID
                    ACCESS_KEY_Secret);

            IAcsClient client = new DefaultAcsClient(profile);
            DescribeDomainRecordsRequest describeDomainRecordsRequest = new DescribeDomainRecordsRequest();

            // 主域名
            describeDomainRecordsRequest.setDomainName(DOMAIN);
            // 主机记录
            describeDomainRecordsRequest.setRRKeyWord(HOME);
            // 解析记录类型
            describeDomainRecordsRequest.setType("A");

            //获取域名的最新解析记录
            DescribeDomainRecordsResponse describeDomainRecordsResponse = client.getAcsResponse(describeDomainRecordsRequest);


            //获取当前最新IP
            String result = HttpUtil.get(IP_QUERY_DOMAIN, null, null);
            String ip = JSON.parseObject(result).getString("data");


            //如果没有解析记录,直接修改
            if (describeDomainRecordsResponse.getDomainRecords().size() == 0) {
                LogUtil.writeInfoLog(HOME + "." + DOMAIN + ",暂无解析记录,请在阿里云域名解析中为该域名修改一次IP值");


                //如果有解析记录
            } else {
                //2.判断IP是否一致，若一致则不执行修改
                String oldIp = describeDomainRecordsResponse.getDomainRecords().get(0).getValue();
                if (ip.equals(oldIp)) {
                    LogUtil.writeInfoLog(HOME + "." + DOMAIN + ",IP暂无变化，无需修改");
                    return;
                }

                //3.IP变化，执行修改
                UpdateDomainRecordRequest updateDomainRecordRequest = new UpdateDomainRecordRequest();

                // 主机记录
                updateDomainRecordRequest.setRR(HOME);

                // 记录ID
                updateDomainRecordRequest.setRecordId(describeDomainRecordsResponse.getDomainRecords().get(0).getRecordId());

                // 将主机记录值改为当前主机IP
                updateDomainRecordRequest.setValue(ip);

                // 解析记录类型
                updateDomainRecordRequest.setType("A");
                UpdateDomainRecordResponse updateDomainRecordResponse = client.getAcsResponse(updateDomainRecordRequest);

                //获取域名的最新解析记录
                DescribeDomainRecordsResponse response2 = client.getAcsResponse(describeDomainRecordsRequest);
                if (response2.getDomainRecords().size() > 0) {
                    LogUtil.writeInfoLog(HOME + "." + DOMAIN + ",已修改IP地址为:" + response2.getDomainRecords().get(0).getValue());
                }
            }

        } catch (Exception e) {
            LogUtil.writeErrorLog(e.getMessage());
        }
    }
}
